package com.bw.module_inspection.view

import android.view.View
import android.widget.PopupMenu
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_base.path.RouterPath
import com.bw.lib_base.view.BaseActivity
import com.bw.module_inspection.R
import com.bw.module_inspection.adapter.InspectionAdapter
import com.bw.module_inspection.databinding.ActivityInspectionBinding
import com.bw.module_inspection.viewmodel.InspectionViewModel

/**
 * 智慧园区-07-巡检管理V1.1 史建军20250512
 */
@Route(path = RouterPath.PATH_INSPECTION_ACTIVITY)
class InspectionActivity : BaseActivity<ActivityInspectionBinding>() {
    private val vm by viewModels<InspectionViewModel> { ViewModelProvider.NewInstanceFactory()  }
    private val inspectionAdapter by lazy { InspectionAdapter() }

    override fun onResume() {
        super.onResume()
        inspectionAdapter.data.clear()
        vm.getInspectionList()


    }
    override fun initView() {
        mDataBinding.topLayout.iconMore.visibility=View.VISIBLE
        mDataBinding.topLayout.iconReturn.setOnClickListener {
            finish()
        }
        mDataBinding.topLayout.tvName.text="点位管理"


        vm.getInspectionList().run {
            vm.inspectionSuccess.observe(this@InspectionActivity){
                inspectionAdapter.data.addAll(it.reversed())
                inspectionAdapter.notifyDataSetChanged()
            }
        }
        //模糊查询
       mDataBinding.iconSearch.setOnClickListener {
           var etInput=mDataBinding.etSearchInput.text.toString()
           if (mDataBinding.etSearchInput.text.length<0){
               vm.getInspectionList().run {
                   vm.inspectionSuccess.observe(this@InspectionActivity){
                       inspectionAdapter.data.clear()
                       inspectionAdapter.data.addAll(it.reversed())
                       inspectionAdapter.notifyDataSetChanged()
                   }
               }
           }else{
               val inputSearch=inspectionAdapter.data.filter { item->
                   item.pointName.contains(etInput)

               }
               inspectionAdapter.data.clear()
               inspectionAdapter.data.addAll(inputSearch)
               inspectionAdapter.notifyDataSetChanged()
           }


       }
        mDataBinding.rvPoint.adapter=inspectionAdapter
        mDataBinding.rvPoint.layoutManager= LinearLayoutManager(this)
        //弹出菜单跳转到新增点位页面
        mDataBinding.topLayout.iconMore.setOnClickListener {
            // 创建popMenu页面
            val popupMenu = PopupMenu(this, it)
            // 加载菜单资源
            popupMenu.menuInflater.inflate(R.menu.menu_inspection, popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { menuItem ->
                when (menuItem.itemId) {
                    R.id.menu1 -> {
                        ARouter.getInstance().build(RouterPath.PATH_INSPECTION_ADD_ACTIVITY).navigation()
                        true
                    }
                    else -> true
                }
            }
            popupMenu.show() // 正确位置，不参与返回值
        }

        inspectionAdapter.setOnItemClickListener { adapter, view, position ->

             ARouter.getInstance().build(RouterPath.PATH_INSPECTION_DETAIL_ACTIVITY).withInt("id",inspectionAdapter.data[position].id).navigation()
        }


    }






    override fun getLayoutId(): Int =R.layout.activity_inspection

}